
#import "BaseJsonResponse.h"
#import "NSObject+SBJson.h"
#import "NSDictionaryUtil.h"

@implementation BaseJSONResponse

-(id)init
{
    self = [super init];
    if (self) {
        _msg = [[NSString alloc] initWithString:@"no message"];
    }
    return self;
}

-(void)dealloc
{
    self.headerDict = nil;
    self.bodyDict = nil;
    [super dealloc];
}

-(void)parse
{
    [super parse];
    
    NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    
    NSString *responseString = [[NSString alloc] initWithData:self.data encoding:gbkEncoding];
    
//    NSString* responseString = [[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding] autorelease];
    if(responseString==nil)
    {
        return;
    }
    
    @try {
        NSDictionary *argsDict = nil;//协议最外层格式一定是json字典
        if ([responseString respondsToSelector:@selector(JSONValue)]) {
            if([[responseString JSONValue] isKindOfClass:[NSDictionary class]]) {
                argsDict = [responseString JSONValue];
                
                if (argsDict)
                {
                    self.headerDict = GetDictionaryDictionaryDefaultNil(argsDict, @"CTSRespHeader");
                    self.bodyDict = GetDictionaryDictionaryDefaultNil(argsDict, @"CTSRespBody");
                    if (self.bodyDict)
                    {
                        self.ret = RET_SUCCESS;
                    }
                }
                
            }
        }
    }
    @catch (NSException *exception) {
        NSLog(@"!!!json解析异常!!!");
        return;
    }
}

@end
